package com.simple.admin.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.simple.framework.constant.HttpStatus;
import com.simple.framework.core.controller.BaseController;
import com.simple.framework.core.domain.R;
import com.simple.framework.core.domain.vo.PagingVo;
import com.simple.system.annotation.SystemLog;
import com.simple.system.domain.dto.AddSysRoleDto;
import com.simple.system.domain.dto.ListSysRoleDto;
import com.simple.system.domain.dto.UpdateRoleDto;
import com.simple.system.domain.entity.SysRole;
import com.simple.system.domain.vo.SysRoleVo;
import com.simple.system.enums.BusinessType;
import com.simple.system.service.SysRoleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 角色管理-控制器
 * @Author xiaoke
 * @Date 2024/8/10 18:00
 * @Version 1.0
 */
@RestController
@RequestMapping("sys_role")
public class SysRoleController extends BaseController {

    private final SysRoleService sysRoleService;

    public SysRoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    /**
     * 分页获取角色列表
     * @param dto 请求参数
     * @return PagingVo<SysRole>
     */
    @GetMapping
    public R<PagingVo<SysRole>> list(ListSysRoleDto dto) {
        Page<SysRole> page = sysRoleService.list(dto);
        return success(new PagingVo<>(page.getRecords(),page.getTotal()));
    }

    /**
     * 根据Id获取角色详情
     * @param id 角色Id
     * @return SysRoleVo
     */
    @GetMapping("{id}")
    public R<SysRoleVo> get(@PathVariable("id") Long id) {
        SysRoleVo sysRoleVo = sysRoleService.get(id);
        return success(sysRoleVo);
    }

    /**
     * 根据Id删除角色
     * @param id 角色Id
     * @return Boolean
     */
    @SystemLog(title = "删除角色",businessType = BusinessType.DELETE)
    @DeleteMapping("{id}")
    public R<Boolean> delete(@PathVariable("id") Long id) {
        Boolean result = sysRoleService.delete(id);
        return success(result);
    }

    /**
     * 新增角色
     * @param dto 请求参数
     * @return 角色Id
     */
    @SystemLog(title = "新增角色",businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> insert(@RequestBody @Validated AddSysRoleDto dto) {
        Long result = sysRoleService.insert(dto);
        if(result==null) {
            return fail(HttpStatus.BAD_REQUEST,"添加角色失败");
        }
        return success(HttpStatus.CREATED,result);
    }

    /**
     * 修改角色
     * @param id 角色Id
     * @param dto 请求参数
     * @return Void
     */
    @SystemLog(title = "修改角色",businessType = BusinessType.UPDATE)
    @PutMapping("{id}")
    public R<Void> update(@PathVariable("id") Long id,@RequestBody @Validated UpdateRoleDto dto) {
        sysRoleService.update(id,dto);
        return success();
    }
}
